package com.yanqu.road.server.manger.activity.luckyshop;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.luckyshop.LuckyShopConfig;
import com.yanqu.road.entity.activity.luckyshop.config.LuckyShopDrawConfig;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.LuckyShopBussiness;
import com.yanqu.road.logic.helper.WeightHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.luckyshop.LuckyShopModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LuckyShopMgr extends TempMgr {

    private static LuckyShopConfig config;

    public static LuckyShopConfig getConfig() {
        return config;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static void  reloadActivityData() {
        getLogger().info("reload LuckyShop Activity start");

        ActivityInfo activityInfo;

        // 获取进行中的活动信息
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.LuckyShop.getValue());
        if (openActivityInfoList.isEmpty() || openActivityInfoList.get(0) == null) {
            getLogger().info("no LuckyShop Activity in show time");
            config = null;
            return;
        } else {
            activityInfo = openActivityInfoList.get(0);
        }
        LuckyShopConfig tmpConfig = new LuckyShopConfig();
        tmpConfig.setActivityInfo(activityInfo);
        int tmpActivityId = activityInfo.getActivityId();
        // config配置
        tmpConfig.setDrawConfigMap(LuckyShopBussiness.getLuckyShopDrawConfig(tmpActivityId));
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
        if (activityConfigMap.containsKey("LUCKYSHOP_BIG_REWARD_POOL")) {
            List<Property> bigRewardList = new ArrayList<>();
            List<String> luckyshopBigRewardPool = activityConfigMap.get("LUCKYSHOP_BIG_REWARD_POOL").getStringListValue("\\|");
            for (String luckyshopBigReward : luckyshopBigRewardPool) {
                bigRewardList.add(PropertyHelper.parseStringToProperty(luckyshopBigReward));
            }
            tmpConfig.setBigRewardList(bigRewardList);
        }
        if (activityConfigMap.containsKey("LUCKYSHOP_BIG_REWARD_TIMES")) {
            tmpConfig.setBigRewardTimes(activityConfigMap.get("LUCKYSHOP_BIG_REWARD_TIMES").getIntValue());
        }
        config = tmpConfig;

        getLogger().info("reload LuckyShop Activity end");

        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            gamePlayer.getModule(LuckyShopModule.class).initSystem();
        }
    }

    /**
     * 获取随机奖励
     */
    public static int randomRewardId() {
        if (config == null) {
            return 0;
        }
        NormalWeightItem randomResult = WeightHelper.getRandomResult(config.getWeightItemList());
        if (randomResult != null) {
            int rewardId = randomResult.getValue().intValue();
            LuckyShopDrawConfig drawConfig = config.getLuckyShopDrawConfig(rewardId);
            if (drawConfig != null) {
                return drawConfig.getId();
            }
        }
        return 0;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
